#include "uart1_interconnect.h"

hi_u32 g_uart_demo_task_id = 0;
hi_u32 count = 0, feed_count = 0;
hi_u32 sum = 5;
hi_u32 tick_count = 0, tick_current = 0;

void drop_cat()
{
    hi_gpio_set_ouput_val(HI_IO_NAME_GPIO_9, HI_GPIO_VALUE1);   
    hi_sleep(20);
    hi_gpio_set_ouput_val(HI_IO_NAME_GPIO_9, HI_GPIO_VALUE0);   
}

void drop_dog()
{
    hi_gpio_set_ouput_val(HI_IO_NAME_GPIO_7, HI_GPIO_VALUE1);   
    hi_sleep(20);
    hi_gpio_set_ouput_val(HI_IO_NAME_GPIO_7, HI_GPIO_VALUE0);   
}

hi_void uart1_interconnect_open()
{
    hi_u32 result;
    //设置串口1的引脚复用功能
    hi_io_set_func(HI_IO_NAME_GPIO_0, HI_IO_FUNC_GPIO_0_UART1_TXD); 
    hi_io_set_func(HI_IO_NAME_GPIO_1, HI_IO_FUNC_GPIO_1_UART1_RXD);
    //喂食引脚
    hi_io_set_func(HI_IO_NAME_GPIO_9, HI_IO_FUNC_GPIO_9_GPIO);
    hi_gpio_set_dir(HI_IO_NAME_GPIO_9, 1);
    hi_gpio_set_ouput_val(HI_IO_NAME_GPIO_9, HI_GPIO_VALUE0);
    hi_io_set_func(HI_IO_NAME_GPIO_7, HI_IO_FUNC_GPIO_7_GPIO);
    hi_gpio_set_dir(HI_IO_NAME_GPIO_7, 1);
    hi_gpio_set_ouput_val(HI_IO_NAME_GPIO_7, HI_GPIO_VALUE0);
    hi_uart_attribute uart_attr = {
        .baud_rate = 115200,
        .data_bits = 8,   
        .stop_bits = 1,
        .parity = 0,
    };
    //初始化串口1，设置波特率等等
    result = hi_uart_init(INTER_CONNECT_UARTNUM, &uart_attr, HI_NULL);
    if (result != HI_ERR_SUCCESS) {
        printf("Failed to init uart1! Err code = %d\n", result);
        return;
    }
    //创建串口任务，任务内容自定义
    hi_task_attr attr = {0};
    attr.stack_size = UART_DEMO_TASK_STAK_SIZE;
    attr.task_prio = UART_DEMO_TASK_PRIORITY;
    attr.task_name = (hi_char*)"uart_demo";
    result = hi_task_create(&g_uart_demo_task_id, &attr, uart_interconnect_task, HI_NULL);
    if (result != HI_ERR_SUCCESS) {
        printf("Falied to create uart demo task!\n");
    }
}

static hi_void *uart_interconnect_task(hi_void *param)
{
    hi_u8 uart_buff[UART_BUFF_SIZE] = {0};
    hi_u8 *uart_buff_ptr = uart_buff;
    hi_unref_param(param);
    printf("Initialize uart interconnect successfully\n");
    for (;;) {
        hi_s32 len = hi_uart_read(INTER_CONNECT_UARTNUM, uart_buff_ptr, UART_BUFF_SIZE);
        if (len > 0) {
#ifdef WRITE_BY_INT
            hi_u8 result = judge_command(uart_buff, len);
            switch (result)
            {
            case PETCAT:
                printf("detected cat!\n");
                count++;
                tick_current = hi_systick_get_real_time();
                // printf("current : %d\n", tick_current);
                // printf("sub : %d\n", tick_current - tick_count);
                if(count >= 10 && feed_count < sum && ((feed_count == 0) || ((tick_current - tick_count) > 10)))
                {
                    tick_count = hi_systick_get_real_time();
                    printf("%ld\n", tick_count);
                    count = 0;
                    feed_count++;
                    char* command[200];
                    sprintf(command, "{\"services\": [{\"service_id\" : \"pet_feeder\",\"properties\" : {\"今日喂食次数\":%d}}]}", feed_count);
                    printf("%s\n",command);
                    IotSendMsg(0, "$oc/devices/{device_id}/sys/properties/report", command);
                    drop_cat();
                }
                break;
            case PETDOG:
                printf("detected dog!\n");
                count++;
                tick_current = hi_systick_get_real_time();
                // printf("current : %d\n", tick_current);
                // printf("sub : %d\n", tick_current - tick_count);
                if(count >= 10 && feed_count < sum && ((feed_count == 0) || ((tick_current - tick_count) > 10)))
                {
                    tick_count = hi_systick_get_real_time();
                    printf("%ld\n", tick_count);
                    count = 0;
                    feed_count++;
                    char* command[200];
                    sprintf(command, "{\"services\": [{\"service_id\" : \"pet_feeder\",\"properties\" : {\"今日喂食次数\":%d}}]}", feed_count);
                    printf("%s\n",command);
                    IotSendMsg(0, "$oc/devices/{device_id}/sys/properties/report", command);
                    drop_dog();
                }
                break;
            default:
                printf("other command\n");
                break;  
            }

            
#else
            hi_uart_write_immediately(DEMO_UART_NUM, uart_buff_ptr, len);
#endif
        } else {
            printf("Read nothing!\n");
            hi_sleep(1000);
        }
    }

    hi_task_delete(g_uart_demo_task_id);
    g_uart_demo_task_id = 0;

    return HI_NULL;
}

hi_u8 judge_command(hi_u8 *data, hi_s32 len)
{
    hi_u8 pet_cat[PET_LENGTH] = {0xaa, 0x55, 0x00, 0x02, 0x00, 0x01, 0xff, 0xbc, 0xe4, 0xdc, 0x5d};
    hi_u8 pet_dog[PET_LENGTH] = {0xaa, 0x55, 0x00, 0x02, 0x00, 0x02, 0xff, 0x97, 0xc9, 0x8f, 0x9e};
    
    if(len == PET_LENGTH) 
    {      
        if(compare(data, pet_cat, PET_LENGTH) == HI_TRUE) 
            return PETCAT;
        else if(compare(data, pet_dog, PET_LENGTH) == HI_TRUE) 
            return PETDOG;   
        else
            return OTHER;        
    }
}

hi_bool compare(hi_u8 *data1, hi_u8 *data2, hi_s32 len)
{
    hi_u8 index = 0;
    for(index = 0; index < PET_LENGTH; ++index)
    {
        if(*(data1 + index) != *(data2 + index)) 
            break;
    }
    if(index < PET_LENGTH)
    {
        return HI_FALSE;
    }
    else
    {
        return HI_TRUE;
    }
}

static hi_void *send_command_task()
{
    hi_uart_write(INTER_CONNECT_UARTNUM, "AT\r\n", sizeof("AT\r\n"));
    hi_sleep(10);
    hi_uart_write(INTER_CONNECT_UARTNUM, "AT+CAVIMS=1\r\n", sizeof("AT+CAVIMS=1\r\n"));
    hi_sleep(10);
    hi_uart_write(INTER_CONNECT_UARTNUM, "AT+GTSET=\"GPRSFIRST\",0\r\n", sizeof("AT+GTSET=\"GPRSFIRST\",0\r\n"));
    hi_sleep(10);
    hi_uart_write(INTER_CONNECT_UARTNUM, "AT+GTSET=\"CALLBREAK\",1\r\n", sizeof("AT+GTSET=\"CALLBREAK\",1\r\n"));
    hi_sleep(10);
    for(;;)
    {
        hi_uart_write(INTER_CONNECT_UARTNUM, "ATA\r\n", sizeof("ATA\r\n")); 
        hi_sleep(200);
    }
}

hi_void send_command()
{
    hi_u32 result = 0;
    hi_task_attr attr = {0};
    attr.stack_size = UART_DEMO_TASK_STAK_SIZE;
    attr.task_prio = UART_DEMO_TASK_PRIORITY;
    attr.task_name = (hi_char*)"uart_demo";
    result = hi_task_create(&g_uart_demo_task_id, &attr, send_command_task, HI_NULL);
    if (result != HI_ERR_SUCCESS) {
        printf("Falied to create uart demo task!\n");
    }
}

static hi_void *key_scan_task()
{
    hi_io_set_func(HI_IO_NAME_GPIO_11, HI_IO_FUNC_GPIO_11_GPIO);
    hi_gpio_set_dir(HI_IO_NAME_GPIO_11, 0);
    hi_io_set_pull(HI_IO_NAME_GPIO_11, HI_IO_PULL_UP);

    hi_io_set_func(HI_IO_NAME_GPIO_12, HI_IO_FUNC_GPIO_12_GPIO);
    hi_gpio_set_dir(HI_IO_NAME_GPIO_12, 0);
    hi_io_set_pull(HI_IO_NAME_GPIO_12, HI_IO_PULL_UP);
    hi_gpio_value value_0 = HI_GPIO_VALUE1;
    hi_gpio_value value_1 = HI_GPIO_VALUE1;
    for(;;)
    {
        hi_uart_write(INTER_CONNECT_UARTNUM, "ATA\r\n", sizeof("ATA\r\n"));
        hi_gpio_get_input_val(HI_IO_NAME_GPIO_11, &value_0);
        if(value_0 == 0)
        {
            hi_sleep(20);
            hi_gpio_get_input_val(HI_IO_NAME_GPIO_11, &value_0);
            if(value_0 == 0)
            {
                drop_cat();
                printf("drop cat\n");
                while(value_0 == 0){
                    hi_gpio_get_input_val(HI_IO_NAME_GPIO_11, &value_0); 
                    hi_sleep(10);
                }
                   
            }
        }
        hi_gpio_get_input_val(HI_IO_NAME_GPIO_12, &value_1);
        if(value_1 == 0)
        {
            hi_sleep(20);
            hi_gpio_get_input_val(HI_IO_NAME_GPIO_12, &value_1);
            if(value_1 == 0)
            {
                drop_dog();
                printf("drop dog\n");
                while(value_1 == 0){
                    hi_gpio_get_input_val(HI_IO_NAME_GPIO_12, &value_1); 
                    hi_sleep(10);
                }
                   
            }
        }
        hi_sleep(200);
    }
}

hi_void key_scan()
{
    hi_u32 result = 0;
    hi_task_attr attr = {0};
    attr.stack_size = UART_DEMO_TASK_STAK_SIZE;
    attr.task_prio = UART_DEMO_TASK_PRIORITY;
    attr.task_name = (hi_char*)"key_scan";
    result = hi_task_create(&g_uart_demo_task_id, &attr, key_scan_task, HI_NULL);
    if (result != HI_ERR_SUCCESS) {
        printf("Falied to create uart demo task!\n");
    }
}

